package edu.hunau.cxb19.oop.class14;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-13 15:08:45
 */
public class NewDateTest {
	@Test
	public void test() {
		LocalDate localDate = LocalDate.now();// 2020-03-13
		System.out.println(localDate);
		LocalTime localTime = LocalTime.now();// 15:10:46.386185300
		System.out.println(localTime);
		LocalDateTime localDateTime = LocalDateTime.now();// 2020-03-13T15:10:46.386185300
		System.out.println(localDateTime);

	}

	@Test
	public void test02() {
		LocalDate localDate = LocalDate.of(2019, 2, 21);
		System.out.println(localDate);
		LocalTime localTime = LocalTime.of(15, 33, 10);
		System.out.println(localTime);
	}

	@Test
	public void test03() {
		LocalDateTime time = LocalDateTime.of(2020, 1, 1, 23, 34, 55);
		int year = time.getYear();
		System.out.println(year);
		int dayOfMonth = time.getDayOfMonth();
		System.out.println(dayOfMonth);
		int dayOfYear = time.getDayOfYear();
		System.out.println(dayOfYear);
		int monthValue = time.getMonthValue();
		System.out.println(monthValue);
		Month month = time.getMonth();// 获得的是一个Month的枚举类型
		System.out.println(month);
	}

	@Test
	public void test04() {
		LocalDateTime time = LocalDateTime.of(2020, 1, 1, 23, 34, 55);
		// LocalDateTime是不可变的，通过方法产生新的日期对象
		LocalDateTime newTime = time.withYear(2019);
		System.out.println(time);// 2020-01-01T23:34:55
		System.out.println(newTime);// 2020-01-01T23:34:55
		newTime = time.withDayOfMonth(31);
		System.out.println(newTime);// 2020-01-31T23:34:55
	}

	@Test
	public void test05() {
		LocalDateTime time = LocalDateTime.of(2020, 1, 1, 23, 34, 55);
		// time 32天之后的时间
		LocalDateTime newTime = time.plusDays(32);
		System.out.println(newTime);// 2020-02-02T23:34:55
		// time之前的1一个月
		newTime = time.minusMonths(1);
		System.out.println(newTime);// 2019-12-01T23:34:55

	}

	@Test
	public void test06() {
		LocalDate localDate = LocalDate.parse("2019-01-01");
		LocalTime localTime = LocalTime.parse("23:59:11");
		System.out.println(localTime);

		// 使用自定义的日期格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy'年'MM'月'dd'日'");
		LocalDate date = LocalDate.parse("2019年12月11日", formatter);
		System.out.println(date);
		System.out.println(date.format(formatter));

	}

	@Test
	public void test07() {
		Instant now = Instant.now();
		System.out.println(now);
		System.out.println(now.getEpochSecond());// 秒数
		System.out.println(now.toEpochMilli());// 毫秒数
		System.out.println(now.getNano());// 纳秒
		System.out.println(System.currentTimeMillis());
	}

	@Test
	public void test08() {
		// 获得ZoneIds中所有的时间日期
		Set<String> zoneIds = ZoneId.getAvailableZoneIds();
		for (String id : zoneIds) {
			// 输出亚洲的时区
			if (id.startsWith("Asia")) {
				System.out.println(id);
			}
		}

		// 指定时区获得时间
		LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
		System.out.println(localDateTime);
	}

	@Test
	public void test09() {
		LocalDateTime time1 = LocalDateTime.of(2020, 1, 1, 23, 34, 55);
		LocalDateTime time2 = LocalDateTime.of(2020, 1, 1, 23, 34, 10);
		Duration duration = Duration.between(time2, time1);
		System.out.println(duration.toSeconds());
		 
	}
	@Test
	public void test10() {
		LocalDate time1 = LocalDate.of(2020, 1, 1);
		LocalDate time2 = LocalDate.of(2022, 1, 1);
		Period period = Period.between(time1, time2);
		System.out.println(period.getYears());
		 
	}
	
	
	@Test
	public void test11() {
		//当前的日期的下一个周日是哪一天
		TemporalAdjuster temporalAdjuster = TemporalAdjusters.next(DayOfWeek.SUNDAY);
		LocalDate date = LocalDate.now().with(temporalAdjuster);
		System.out.println(date);
	}

}
